package com.microinvest.util;
import jakarta.mail.Message;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

//此工具类用于生成发送邮箱验证码
@Component
public class MailVerification {

    @Autowired
    private JavaMailSender mailSender;

    public void sendMail(String userMail,String num) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                mimeMessage.setRecipient(Message.RecipientType.TO,
                        new InternetAddress(userMail));
                mimeMessage.setFrom(new InternetAddress("3598018258@qq.com"));
                mimeMessage.setText( userMail+"：您好，您的一次性验证码为:"+num+ "，时效为五分钟");
            }
        };

        try {
            this.mailSender.send(preparator);
        }

        catch (MailException ex) {
            System.err.println(ex.getMessage());
        }
    }
}
